package com.sunspring.zdcall.service.impl;

import com.sunspring.zdcall.common.constant.enums.StateEnum;
import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.bo.SourceMaxTime;
import com.sunspring.zdcall.entity.ZdBaseCheckroom;
import com.sunspring.zdcall.entity.ZdBaseDepartment;
import com.sunspring.zdcall.entity.ZdQueueInfo;
import com.sunspring.zdcall.mapper.master.CheckRoomMapper;
import com.sunspring.zdcall.mapper.master.QueueMapper;
import com.sunspring.zdcall.mapper.slave.RegisterQueueMapper;
import com.sunspring.zdcall.service.CheckRoomService;
import com.sunspring.zdcall.service.DeptService;
import com.sunspring.zdcall.service.QueueService;
import com.sunspring.zdcall.support.cache.DeptCache;
import com.sunspring.zdcall.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author WuLei
 * @Date 2018/11/27  16:55
 */
@Service
@Slf4j
public class QueueServiceImpl implements QueueService {

    @Autowired
    private RegisterQueueMapper registerQueueMapper;

    @Autowired
    private QueueMapper queueMapper;

    @Autowired
    private DeptService deptService;

    @Autowired
    private CheckRoomService crs;

    @Autowired
    private CheckRoomMapper checkRoomMapper;

    @Override
    public void addBatch(List<ZdQueueInfo> list) {
        int result = queueMapper.insertBatchSelective(list);
        if (result <= 0){
            throw new ZdException("新增队列信息失败，队列信息："+list);
        }
    }

    @Override
    public List<ZdQueueInfo> getQueueFromApi(Date date) {
        return registerQueueMapper.selectByRegisterTime(date);
    }


    @Override
    public void changeCheckStatus(String inspectId, int state) {
        if((queueMapper.updateCheckStatus(inspectId,state)) <= 0) {
            throw new ZdException("修改诊断状态错误");
        }
    }

    @Override
    public void finishVisit(String inspectId, Date time) {
        if((queueMapper.finish(inspectId,time)) <= 0) {
            throw new ZdException("修改诊断状态错误");
        }
    }

    @Override
    public List<SourceMaxTime> findMaxCheckInTime() {
        return queueMapper.findMaxCheckInTime(TimeUtil.todayStartDate(), TimeUtil.todayEndDate());
    }

    @Override
    public List<ZdQueueInfo> findTodayQueue() {
        return queueMapper.selectTodayInfo(TimeUtil.todayStartDate(), TimeUtil.todayEndDate());
    }

    @Override
    public void updatePrintStatus(String inspectId,int status) {
        if((queueMapper.updatePrintStatus(inspectId,status)) <= 0) {
            throw new ZdException("修改打印状态失败，inspectId={}：" + inspectId);
        }
    }

    @Override
    public String findMaxQueueId(String checkId,Date time) {
        return queueMapper.selectMaxQueueId(checkId,time);
    }


    @Override
    public int add(ZdQueueInfo queueInfo) {
        return queueMapper.add(queueInfo);
    }

    @Override
    public ZdBaseCheckroom deptCheck(ZdQueueInfo queueInfo) {
        ZdBaseDepartment department = deptService.selectById(queueInfo.getDepartmentId());
        //科室不存在，新增科室
        if (department == null) {
            department = new ZdBaseDepartment(queueInfo.getDepartmentId(),
                    queueInfo.getDepartment(),
                    queueInfo.getDepartment());
            deptService.addDept(department);
            //科室状态为空 丢弃数据
        }else if (department.getDepartmentState() == StateEnum.disable.getCode()) {
            log.info("科室:{}已停用,数据已丢弃",department.getDepartmentId());
            return null;
        }
        ZdBaseCheckroom checkroom = crs.getById(queueInfo.getCheckId());
        //检查室不存在，新增检查室
        if (checkroom == null) {
            checkroom = new ZdBaseCheckroom();
            checkroom.setDepartmentId(queueInfo.getDepartmentId());
            checkroom.setCheckRoomDisplayName(queueInfo.getCheckRoom());
            checkroom.setCheckRoomName(queueInfo.getCheckRoom());
            checkroom.setDepartmentId(queueInfo.getDepartmentId());
            int count = checkRoomMapper.selectCountByDeptId(queueInfo.getDepartmentId());
            checkroom.setQueuePrefix(Integer.toString(count + 1));
            checkroom.setQueueNumber(3);
            crs.addCheck(checkroom);
        }else if (checkroom.getCheckroomState() == StateEnum.disable.getCode()) {
            log.info("检查室:{}已停用,数据已丢弃",checkroom.getCheckRoomId());
            return null;
        }
        return checkroom;
    }
}
